package com.hifar.test.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hifar.sys.BusiResponse;
import com.hifar.test.test.pojo.HfEnvTestCurveData;
import com.hifar.test.test.pojo.VO.HfEnvCurveDataSaveVO;
import com.hifar.test.test.service.IHfEnvTestCurveDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuWeiWei
 * @description 曲线
 * @date 2025/6/10 17:26
 */
@RestController()
@RequestMapping("/api/busi/test/testCurveData")
@Api(tags = "试验曲线数据")
public class HfEnvTestCurveDataController {

    @Autowired
    private IHfEnvTestCurveDataService testCurveDataService;

    @GetMapping("/listCurveData")
    @ApiOperation("查询试验曲线数据")
    public BusiResponse listCurveData(@RequestParam("testId") String testId) {

        List<HfEnvTestCurveData> list = testCurveDataService.lambdaQuery()
                .eq(HfEnvTestCurveData::getTestId, testId)
                .orderByAsc(HfEnvTestCurveData::getRowSort)
                .list();
        return BusiResponse.OK(list);
    }

    @ApiOperation("保存试验曲线数据")
    @PostMapping("/saveCurveData")
    public BusiResponse saveCurveData(@RequestBody @Validated HfEnvCurveDataSaveVO hfEnvCurveDataSaveVO) {
        String testId = hfEnvCurveDataSaveVO.getTestId();
        List<HfEnvTestCurveData> curveData = hfEnvCurveDataSaveVO.getCurveData();

        QueryWrapper<HfEnvTestCurveData> curveQueryWrapper = new QueryWrapper<>();
        curveQueryWrapper.lambda()
                .eq(HfEnvTestCurveData::getTestId, testId);
        testCurveDataService.remove(curveQueryWrapper);

        for (int i = 0; i < curveData.size(); i++) {
            HfEnvTestCurveData hfEnvTestCurveData = curveData.get(i);
            hfEnvTestCurveData.setTestId(testId);
            hfEnvTestCurveData.setRowSort(i);
        }
        testCurveDataService.saveBatch(curveData);

        return BusiResponse.OK();
    }
}
